<?php

namespace common\services;

use common\models\StockProductCase;
use Yii;
use yii\data\Pagination;

class TallyProductStockCaseServices extends BaseService
{
    /**
     * 缓存存储KEY
     */
    const CACHE_BUCKET = 'TallyProductStockCase:';

    /**
     * 缓存有效期
     */
    const CACHE_TIMEOUT = 300; // 5min

    /**
     * Created by PhpStorm.
     * @purpose
     * @Author: fhc
     * @Time: 2023/3/27 15:32
     */
    public static function getList($tallyId, $condition)
    {
        $where = [
            'tally_id' => $tallyId,
        ];
        $query = StockProductCase::find()->where($where);

        $count = $query->count();
        // 构造分页对象
        $pageSize = $_SERVER['PAGE_SIZE'] ?: 10 ;
        $pagination = new Pagination([
            'defaultPageSize' => $pageSize,
            'totalCount' => $count
        ]);
        $pagination->validatePage=false;

        // 从DB获取
        $houseList = $query->orderBy('id desc')
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->asArray()
            ->all();

        // 返回带分页数据
        return [
            'data'       => $houseList ?: [],
            'pageInfo'   => [
                'totalCount' => intval($count) ,
                'pageSize'   => intval($pageSize)
            ]
        ];
    }

    /**
     * Created by PhpStorm.
     * @purpose
     * @Author: fhc
     * @Time: 2023/3/27 17:14
     */
    public static function create(array $formData)
    {
        $model = new StockProductCase();
        $model->load($formData,'');
        if ($model->validate() && $model->save()) {
            return $model->id ;
        }else{
            Yii::error('TallyStore insert error:'.current($model->getFirstErrors()).';post:'.Json::encode($formData,JSON_UNESCAPED_UNICODE));
            self::setError(current($model->getFirstErrors()));
            return false;
        }
    }

    /**
     * Created by PhpStorm.
     * @purpose
     * @Author: fhc
     * @Time: 2023/3/27 17:30
     */
    public static function update(array $toArray)
    {
        $model = StockProductCase::find()->where([
            'id'=>$toArray['id']
        ])->one();
        if(!$model){
            self::setError('项目不存在');
            return false ;
        }
        unset($toArray['id']);
        $model->load($toArray,'');
        if ($model->validate() && $model->save()) {
            return true ;
        }else{
            Yii::error('StockProduct update error:'.current($model->getFirstErrors()).';post:'.Json::encode($formData,JSON_UNESCAPED_UNICODE));
            self::setError(current($model->getFirstErrors()));
            return false;
        }
    }

    /**
     * Created by PhpStorm.
     * @purpose
     * @Author: fhc
     * @Time: 2023/3/27 17:34
     */
    public static function delete(array $array)
    {
        $model = StockProductCase::find()->where([
            'id'=>$array['id']
        ])->one();
        if(!$model){
            self::setError('单位不存在');
            return false ;
        }
        if ($model->delete()) {
            return true ;
        }else{
            self::setError(current($model->getFirstErrors()));
            return false;
        }
    }

    public static function getAll($tallyId)
    {
        $where = [
            'tally_id' => $tallyId,
        ];
        $query = StockProductCase::find()->where($where);
        return $query->all();
    }


}
